setwd("C:\\Users\\kogan.18\\Box\\Projects\\R_Workfolder\\Foodstamp Rollout\\Replication Data")

##Figure 1
fs<-read.dta("fsrollout.dta")

t<-fs
t$Dummy<-0
t$Dummy[t$fs_year<=1960]<-1
t$Year<-1960
t<-subset(t, select=c("fips","Dummy","Year"))

for (i in 1961:1977){
t2<-fs
t2$Dummy<-0
t2$Dummy[t2$fs_year<=i]<-1
t2$Year<-i
t2<-subset(t2, select=c("fips","Dummy","Year"))
t<-rbind(t,t2)
}

pop<-read.dta("1970pop.dta")
pop$fips<-as.numeric(as.character(pop$fips))
pop<-subset(pop, is.na(pop1970)==FALSE)

tn<-merge(t,pop, by="fips",all.x=TRUE)
tn<-subset(tn, is.na(fips)==FALSE & is.na(state_fips)==FALSE)

tn$share<-(tn$Dummy*tn$pop1970)/202935106*100

hold<-aggregate(tn$share, by=list(tn$Year), FUN="sum")
names(hold)<-c("Year","Share")
hold<-subset(hold, Year<1976)

pdf("rollout.pdf", height=6, width=8)
plot(hold$Year, hold$Share, type="l", xlab="Year", ylab="Food Stamp Coverage (% of 1970 Population)",ann=TRUE, axes=FALSE)
axis(1,at = seq(1960,1976, by = 1), label = seq(1960,1976, by = 1), cex.axis = 1)#add x-axis and labels; "pretty" creates a sequence of  equally spaced nice values that cover the range of the values in 'x'-- in this case, integers
axis(2)
box()
abline(h=0,col="lightgray")
abline(h=20,col="lightgray")
abline(h=40,col="lightgray")
abline(h=60,col="lightgray")
abline(h=80,col="lightgray")
abline(h=100,col="lightgray")
lines(hold$Year, hold$Share)
points(hold$Year, hold$Share, cex=1.5, pch=16)
dev.off()



##Figure 2
pdf("Example.pdf", height=4, width=8)
par(mar=c(2.1,2.1,2.1,8.1))
plot(0,0, type="n", ylim=c(0,3), xlim=c(0,10), ylab="", xlab="Year", ann=TRUE, axes=FALSE)
axis(1,at = seq(0,10, by = 1), label = seq(1960,1970, by = 1), cex.axis = 0.9)#add x-axis and labels; "pretty" creates a sequence of  equally spaced nice values that cover the range of the values in 'x'-- in this case, integers
axis(2, lwd.ticks=0,label=NA, cex.axis = 0.9)
rect(1.5, 2.25, 11, 2.75, col="grey", lwd=0)
rect(3.5, 1.25, 11, 1.75, col="grey", lwd=0)
rect(7.25, 0.25, 11, 0.75, col="grey", lwd=0)
abline(v=0, lwd = 1, col = "black", lty="dashed")
abline(v=4, lwd = 1, col = "black", lty="dashed")
abline(v=8, lwd = 1, col = "black", lty="dashed")
box()
par(las=1)
mtext("Otero, NM", at=0.5, side = 4, adj=0, line=0.25)
mtext("Santa Fe, NM", at=1.5, side = 4, adj=0, line=0.25)
mtext("Santa Fe, NM", at=1.5, side = 4, adj=0, line=0.25)
mtext("San Miguel, NM", at=2.5, side = 4, adj=0, line=0.25)
dev.off()

##Figure 3
hold<-read.dta("ForFig3.dta")
hold$change_share<-hold$dem_share-hold$lag_dem_share
ave_state<-aggregate(hold$change_share, by=list(hold$FIPS_State,hold$Elect_Year), FUN="mean", na.action=na.omit)
names(ave_state)<-c("FIPS_State","Elect_Year", "St_Ye_FE")
hold<-subset(hold, is.na(hold$change_share)==FALSE)

hold<-subset(hold, Elect_Year==1968 | Elect_Year==1972 | Elect_Year==1976)

hold<-merge(hold, ave_state, by=c("Elect_Year","FIPS_State"))
hold$change_share2<-hold$change_share-hold$St_Ye_FE
hold$rollout_change<-factor(hold$rollout_change, levels=c(0,1), labels=c("No","Yes"))

setwd("C:\\Users\\kogan.18\\Box Sync\\Projects\\R_Workfolder\\Foodstamp Rollout\\Paper\\Figures")
pdf("elections.pdf", width=6, height=3)
library(ggplot2)
ggplot(hold, aes(y=change_share2, x=as.factor(rollout_change))) +  
 stat_summary(fun.y="mean", geom="bar")+
    facet_wrap(~Elect_Year)+xlab("Implemented FSP Since Last Election?") +
  ylab("Change in Democratic Vote\nShare Since Last Election")+ theme_bw()+
theme(plot.margin = unit(c(5.5, 5.5, 5.5, 15), "points"))
dev.off()

##Figure 4
dat<-read.csv("event_study_Preferred.csv")

library(stringr)
pdf("event_study.pdf", height=4, width=6)
par(oma=c(0,0,0,0), mar=c(4.1,4.1,2,2))
plot(0,0, type="n", ylim=c(-1,3), xlim=c(-3,4), ylab="Effect on Dem. Vote Share", xlab="Years Since FSP Implementation", ann=TRUE, axes=FALSE)
axis(1,at = seq(-3,4, by = 1), label = c(seq(-3,3, by = 1),"4+"), cex.axis = 0.9)#add x-axis and labels; "pretty" creates a sequence of  equally spaced nice values that cover the range of the values in 'x'-- in this case, integers
axis(2, , cex.axis = 0.9)
box()
abline(h=0, lwd = .5, col = "black", lty="dashed")
for(i in 0:6){
segments(x0=-3+i,y0=as.numeric(as.character(dat[2+(i*2),2])), x1=-2+i,y1=as.numeric(as.character(dat[4+(i*2),2])),lwd=1.5)#draw lines connecting 95% confidence intervals
}
for(i in 0:7){
segments(x0=-3+i,y0=as.numeric(str_split_fixed(dat[3+(i*2),2],",",2))[1], x1=-3+i,y1=as.numeric(str_split_fixed(dat[3+(i*2),2],",",2))[2],col="gray")#draw lines connecting 95% confidence intervals
points(y=as.numeric(as.character(dat[2+(i*2),2])), x=-3+i, pch=15, xpd=TRUE, cex=1, col="black", bg="white")
}
dev.off()
